using MediatR;
using SmartApplication.Api.Applient.Command.Kill;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;

namespace SmartApplication.Api.Applient.CommandHandler.Kill
{
    public class UpdateBusinessHandler : IRequestHandler<UpdateBusinessCommand, ApiResult<int>>
    {
        public UpdateBusinessHandler(IBaseRepository<BusinessModel> businessRepository)
        {
            BusinessRepository = businessRepository;
        }

        public IBaseRepository<BusinessModel> BusinessRepository { get; }

        /// <summary>
        /// 修改屠宰企业信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(UpdateBusinessCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();

            var business = await BusinessRepository.GetValueAsync(request.Id);
            business.BusinessName = request.BusinessName;
            business.CreateDate = request.CreateDate;
            business.BusinessAdd = request.BusinessAdd;
            business.Introduction = request.Introduction;
            business.BusinessPic = request.BusinessPic;

            var update = await BusinessRepository.UpdateAsync(business);
            if (update > 0)
            {
                res.Code = EnumCode.Success;
                res.Message = "修改屠宰企业信息成功";
                res.Data = update;
                return res;
            }
            else
            {
                res.Code = EnumCode.Fail;
                res.Message = "修改屠宰企业信息失败";
                res.Data = update;
                return res;
            }
        }
    }
}